package com.application.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.DTO.PartDTO;
import com.application.DataProvider.DetailLearningDAO;
import com.application.DataProvider.PartDAO;

public class FrmDetailPart extends Activity{

	private int _idPart;
	private int _idPartPre;
	private int _idPartNext;
	private int _idDetailLearning;
	private int _idChap;
	private PartDTO pDto;
	private PartDAO pDao;
	private static ImageView _btnTest;
	private static ImageView _btnFeedback;
	private TextView _tvNamePart;
	private TextView _tvContentPart;
	private ImageButton _btnNext;
	private ImageButton _btnPre;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_detailpart);

		_btnTest = (ImageView)findViewById(R.id.detailpart_tv_imgfulltest);
		_btnFeedback = (ImageView)findViewById(R.id.detailpart_tv_imgfeedback);
		_btnNext = (ImageButton)findViewById(R.id.detailpart_btnNext);
		_btnPre = (ImageButton)findViewById(R.id.detailpart_btnPrevious);

		_tvNamePart = (TextView)findViewById(R.id.detailpart_tvTitle);
		_tvContentPart = (TextView)findViewById(R.id.detailpart_tvContent);
		pDto = new PartDTO();
		//Lay duoc ID, IDChapter tu FrmPart
		Bundle bBundle = this.getIntent().getExtras();	
		_idPart = Integer.valueOf(bBundle.getString("id"));
		pDao = new PartDAO(null);
		showDetailPart();

		_btnTest.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent iIntent = new Intent(FrmDetailPart.this,FrmListTest.class);
				FrmListTest.set_idDetailLearning(_idDetailLearning);
				FrmListTest.set_idPart(_idPart);
				Bundle bBun = new Bundle();
				iIntent.putExtras(bBun);
				StackActivity activityStack = (StackActivity) getParent();
				activityStack.push("FrmListTest", iIntent);


			}
		});

		//		_btnFeedback.setOnClickListener(new OnClickListener() {
		//
		//			@Override
		//			public void onClick(View v) {
		//				// TODO Auto-generated method stub
		//				FrmDoFeedback.set_idPart(_idPart);
		//				FrmDoFeedback.set_idChap(0);
		//				startActivity(new Intent(ActivityController.getPeerActivity().getBaseContext(),FrmDoFeedback.class));
		//			}
		//		});

		//Bat su kien khi nhan vao button Previous
		_btnPre.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				_idPartNext = _idPart;
				_idPart = _idPartPre;
				_idPartPre = pDao.getIDPartPre(_idPart, _idChap);
				Log.d("PEERDROID","depart:" + _idPart);
				Log.d("PEERDROID","depart pre:" + _idPartPre);
				Log.d("PEERDROID","depart next:" + _idPartNext);
				ActivityController.handler.post(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						updateDetailPart();
					}
				});
			}
		});

		//Bat su kien khi nhan vao button Next
		_btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				_idPartPre = _idPart;
				_idPart = _idPartNext;
				_idPartNext = pDao.getIDPartNext(_idPart, _idChap);
				Log.d("PEERDROID","depart:" + _idPart);
				Log.d("PEERDROID","depart next:" + _idPartNext);
				Log.d("PEERDROID","depart pre:" + _idPartPre);
				ActivityController.handler.post(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						updateDetailPart();
					}
				});
			}
		});
	}

	public void showDetailPart()
	{
		_idChap = pDao.getIDChap(_idPart);
		_idPartPre = pDao.getIDPartPre(_idPart, _idChap);
		_idPartNext = pDao.getIDPartNext(_idPart, _idChap);

		_btnNext.setEnabled(true);
		_btnPre.setEnabled(true);
		if(_idPartPre == 0)
			_btnPre.setEnabled(false);
		if(_idPartNext == -1)
			_btnNext.setEnabled(false);
		//bài tập + feedback
		Log.d("PEERDROID","id part frm depart:" + _idPart);
		pDto = pDao.getPart(_idPart);

		DetailLearningDAO dlDao = new DetailLearningDAO(null);
		_idDetailLearning = dlDao.getIDDetailLearning(_idPart, NetworkController.get_idClass());
		Log.d("PEERDROID","_idDetailLearning frm depart:" + _idDetailLearning);
		int idFeelType = dlDao.getIDFeelType(_idDetailLearning); //co feeltype hay ko

		//boolean statusFeedback = dlDao.getStatus(_idDetailLearning);
		if(idFeelType == 0)
		{
			_btnFeedback.setVisibility(View.INVISIBLE);
		}
		else{
			_btnFeedback.setVisibility(View.VISIBLE);
			//			_btnFeedback.setEnabled(false);
		}
		if(!ReadWriteFileController.isExistTestInTestList(_idPart, _idDetailLearning))
			_btnTest.setVisibility(View.INVISIBLE);
		else 
			_btnTest.setVisibility(View.VISIBLE);
		//tiêu đề
		_tvNamePart.setText(pDto.get_Name());

		//nội dung
		_tvContentPart.setText(pDto.get_Content());
	}

	public void updateDetailPart()
	{
		_idChap = pDao.getIDChap(_idPart);
		_btnNext.setEnabled(true);
		_btnPre.setEnabled(true);
		Log.d("PEERDROID","id part pre :" + _idPartPre);
		Log.d("PEERDROID","id part next :" + _idPartNext);
		if(_idPartPre == 0)
			_btnPre.setEnabled(false);
		if(_idPartNext == -1)
			_btnNext.setEnabled(false);

		//bài tập + feedback
		_idChap = pDao.getIDChap(_idPart);
		pDto = pDao.getPart(_idPart);

		DetailLearningDAO dlDao = new DetailLearningDAO(null);
		_idDetailLearning = dlDao.getIDDetailLearning(_idPart, NetworkController.get_idClass());

		int idFeelType = dlDao.getIDFeelType(_idDetailLearning); //co feeltype hay ko
		boolean statusFeedback = dlDao.getStatus(_idDetailLearning);
		if(idFeelType == 0)
		{

			_btnFeedback.setVisibility(View.INVISIBLE);
		}
		else{
			if(!statusFeedback)
			{
				_btnFeedback.setImageResource(R.drawable.img_phanhoi_disable);
				_btnFeedback.setEnabled(false);
			}
			else
			{
				_btnFeedback.setImageResource(R.drawable.img_phanhoi_enable);
				_btnFeedback.setEnabled(true);
			}
		}
		if(!ReadWriteFileController.isExistTestInTestList(_idPart, _idDetailLearning))
			_btnTest.setVisibility(View.INVISIBLE);
		else 
			_btnTest.setVisibility(View.VISIBLE);
		//tiêu đề
		_tvNamePart.setText(pDto.get_Name());

		//nội dung
		_tvContentPart.setText(pDto.get_Content());
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("PEERDROID","vao onstart detail part");
		//ActivityController.setPeerActivity(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
	//	ActivityController.setPeerActivity(this);
	}
	
	@Override
	public void onPause(){
		super.onPause();
	//	ActivityController.setPeerActivity(this);
	}
	public static void updateDetailPart(boolean isFeedback, boolean isTest)
	{
		if(!isFeedback)
		{
			_btnFeedback.setVisibility(View.INVISIBLE);
		}

		if(!isTest)
			_btnTest.setVisibility(View.INVISIBLE);
	}

}
